package com.shoulder.launcher.autoconfigure;

import com.shoulder.imports.Imports;
import com.shoulder.launcher.listener.LauncherRequestListener;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;

@Imports(value = AutoConfiguration.class)
@AutoConfiguration
public class LauncherAutoConfiguration {

    @Bean
    public LauncherRequestListener launcherRequestListener() {
        return new LauncherRequestListener();
    }

    // 过滤器跨域配置
    //todo 因为引入了 spring authorization server,此处的配置不再生效，如果不引入授权服务器 可以将此代开
    //
//    @Bean
//    public FilterRegistrationBean<CorsFilter> launcherCorsFilterBean() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        CorsConfiguration config = new CorsConfiguration();
//        // 允许跨域的头部信息
//        config.addAllowedHeader("*");
//        // 允许跨域的方法
//        config.addAllowedMethod("*");
//        // 需要跨域用户凭证（cookie、HTTP认证及客户端SSL证明等）
//        config.setAllowCredentials(true);
//        config.addAllowedOriginPattern("*");
//        // 跨域路径配置
//        source.registerCorsConfiguration("/**", config);
//        return createFilterRegistrationBean(new CorsFilter(source), FilterEnums.CORS_FILTER.getOrder(),  FilterEnums.CORS_FILTER.getCode());
//    }


    //todo 因为引入了 spring authorization server,此处的配置转移至spring security，如果不引入授权服务器 可以将此代开
//    @Bean
//    @ConditionalOnProperty(value = "launcher.xss.enable", matchIfMissing = true, havingValue = "true")
//    public FilterRegistrationBean<LauncherXssFilter> xssFilterRegistration(XssProperties xssProperties) {
//        FilterRegistrationBean<LauncherXssFilter> registration =
//                createFilterRegistrationBean(new LauncherXssFilter(), FilterEnums.XSS_FILTER.getOrder(),  FilterEnums.XSS_FILTER.getCode());
//        registration.setDispatcherTypes(DispatcherType.REQUEST);
//        //添加过滤路径
//        registration.addUrlPatterns("/**");
//        //设置初始化参数
//        Map<String, String> initParameters = new HashMap<>();
//        initParameters.put(LauncherXssFilter.EXCLUDE_URLS, CollectionUtils.isEmpty(xssProperties.getExcludeUrls())
//                ? "" : String.join(",", xssProperties.getExcludeUrls()));
//        registration.setInitParameters(initParameters);
//        return registration;
//    }
}
